package parser;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;

public class Baza 
{
    
    public static void toDatabase(ArrayList<Stancja> list) 
    {

        Connection con = null;
        Statement st = null;
        
        String url = "jdbc:mysql://127.0.0.1:3306/cruel_stancje";
        String user = "root";
        String password = "47EFea5477";

        try {

            con = DriverManager.getConnection(url, user, password);

            st = con.createStatement();

            for (int i=0; i<list.size(); i++) {
                Stancja s=(Stancja)list.get(i);
                String query = "INSERT INTO Stancje(Id,Tytul,Adres,Data_dodania,Cena,Opis) VALUES("+null+",'"+s.getTytul()+"','"+s.getAdres()+"','"+s.getData()+"',"+s.getCena()+",'"+s.getOpis()+"')";
                st.executeUpdate(query);
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Baza.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {

            try {
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Baza.class.getName());
                lgr.log(Level.SEVERE, ex.getMessage(), ex);
            }
        }
   }
  
    public static void main(String args[])
    {
        Parser parser = new Parser();
        parser.findAll();
        ArrayList<Stancja> list = parser.removeUnnecessery(parser.getArray());
        toDatabase(list);
        parser.getArray().clear();
    }
}


